package view.projectTeam;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import dal.project.Project;
import dal.project.ProjectUtil;
import dal.project_team.ProjectTeam;
import dal.project_team.ProjectTeamUtil;

/**
 * Servlet implementation class ComboProjectId
 */
@WebServlet("/ComboProjectId")
public class ComboProjectId extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ComboProjectId() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter printWriter=response.getWriter();
		response.setContentType("text/HTML");
		
		response.setHeader("Cache-control", "no-cache, no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");
		
		response.setHeader("Access-Control-Allow-Origins", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
	    response.setHeader("Access-Control-Max-Age", "86400");
		
	    Gson gson=new Gson();
		JsonObject myJsonObj=new JsonObject();
		String statusText="";
		
		try {
			
			
			String ProjectId=request.getParameter("project_id");
			
			Project project=new Project();
			ProjectUtil projectUtil=new ProjectUtil();
			
			System.out.println("From servlet ComboProjectId"+ProjectId);
			project.setProjectId(ProjectId);
			//Boolean isOk=null;
			
			
			Object object=projectUtil.get(ProjectId);
			HttpSession session = request.getSession();
			session.setAttribute("project", object);
			System.out.println(object);
			request.getRequestDispatcher("ProjectTeamAdd.jsp").forward(
					request, response);
			
			
			
			
		} catch (Exception e) {
			myJsonObj.addProperty("success", false);
			statusText= "<b>"
					+ " Project Team Information Not Added Succesfully "
					+ "</b>";
			e.printStackTrace();
			// TODO: handle exception
		}
		
		myJsonObj.addProperty("statusText", statusText);
		printWriter.println(myJsonObj.toString());
		printWriter.close();
		
		// TODO Auto-generated method stub
		
	}
}
